package puzzle.game.states;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import puzzle.core.LevelData;
import puzzle.core.LevelMap;
import puzzle.game.BallPuzzle;
import puzzle.game.MapList;
import puzzle.game.states.IngameState;
import puzzle.game.states.LoadingState;
import puzzle.util.TimerAction;

public class StandardIngameState extends IngameState {
  
  public static final int ID = 3;
  
  private TimerAction startLock = new TimerAction(900);
  private boolean showLevel = false;
  private final Color textColor = new Color(163, 209, 242);
  private boolean locked = false;
  protected String winStr = "You win!";
  private boolean ending = false;
  
  public StandardIngameState(BallPuzzle ctx) {
    super(ctx);
    startLock.setRepeats(false);
  }
  
  public void enter(GameContainer container, StateBasedGame game) throws SlickException {
    super.enter(container, game);
    lock(); 
  }
  
  private void lock() {
    locked = true;
    showLevel = true;
    startLock.restart();
    textColor.a = 1.0f;
  }
  
  public String getMapText(MapList.Entry e) {
    return e.getData().getName();
  }
  
  public void endLevel() {
    if (maps.size()!=0) {
      currentMap++;
      if (currentMap > maps.size()-1) {
        currentMap = maps.size()-1;
        endGame();
        return;
      }
      else {
        lock();
      }
    }
  }
  
  public void endGame() {
    ending = true;
    locked = true;
    startLock.restart();
    textColor.a = 1.0f;
  }
  
  public void renderState(GameContainer c, Graphics g) {
    super.renderState(c, g);
    
    Font oldFont = g.getFont();
    
    if (showLevel) {
      g.setFont(getContext().getLoadingState().getMainFont());
      Font font = g.getFont();
      String str = levelStr+" "+(currentMap+1);
      int ctx = c.getWidth()/2 - font.getWidth(str)/2;
      int cty = c.getHeight()/2 - font.getHeight(str)/2;
      
      Color old = g.getColor();
      g.setColor(textColor);
      g.drawString(str, ctx, cty);
      g.setColor(old);
    }
    else if (ending) {
      g.setFont(getContext().getLoadingState().getMainFont());
      Font font = g.getFont();
      int ctx = c.getWidth()/2 - font.getWidth(winStr)/2;
      int cty = c.getHeight()/2 - font.getHeight(winStr)/2;
      
      Color old = g.getColor();
      g.setColor(textColor);
      g.drawString(winStr, ctx, cty);
      g.setColor(old);
    }
    
    g.setFont(oldFont);
  }
  
  public void updateState(GameContainer c, int delta) {
    if (locked) {
      if (ending)
        super.updateState(c, delta);
      
      if (startLock.update(delta)) {
        locked = false;
        
        //if we now change states
        if (ending) {
          getContext().enterState(MenuState.ID, 
                        getExitTransition(), 
                        getEnterTransition());
        }
      }
      else 
        return;
    }
    else 
      super.updateState(c, delta);
    
    if (showLevel||ending) {
      textColor.a -= delta * 0.002f;
      if (textColor.a <= 0) {
        textColor.a = 0;
        if (showLevel) showLevel = false;
        if (ending) ending = false;
      }
    }
  }
  
  public MapList getStateMapList() {
    return getContext().getLoadingState().getGameMapList();
  }
  
  public int getID() {
    return ID;
  }
  
  public void leave(GameContainer container, StateBasedGame game) throws SlickException {
    if (ending) {
      ending = false;
      currentMap = 0;
    }
    
    super.leave(container, game);
  }
  

  public void setMapIndex(int i)
  {
	// TODO Auto-generated method stub
	
  }


}
